package cn.com.analyze;

import java.util.List;

import org.jnetpcap.packet.JPacket;
import org.jnetpcap.packet.format.FormatUtils;
import org.jnetpcap.protocol.network.Ip4;

public class TotalBytes {
	private List<JPacket> packets;
	int total;
	int total_send;
	int total_recv;
	final int COOKED_HEAD_LEN = 16;
	
	public TotalBytes(){}
	public TotalBytes(List<JPacket> lst)
	{
		packets = lst;
		total = 0;
		total_send = 0;
		total_recv = 0;
	}
	
	Ip4 ip4 = new Ip4();
	String src = null;
	String dst = null;
	public void calculate()
	{
		for(JPacket packet : packets)
		{
			if(packet.hasHeader(Ip4.ID))
			{
				packet.getHeader(ip4);
				src = FormatUtils.ip(ip4.source());
				dst = FormatUtils.ip(ip4.destination());
				if(src.equals(Parser.hostip))
					total_send += packet.size() - COOKED_HEAD_LEN;
				else if(dst.equals(Parser.hostip))
					total_recv += packet.size() - COOKED_HEAD_LEN;
			}	
		}
	}
	
	public void add(JPacket packet)
	{
		if(packet.hasHeader(Ip4.ID))
		{
			packet.getHeader(ip4);
			src = FormatUtils.ip(ip4.source());
			dst = FormatUtils.ip(ip4.destination());
			if(src.equals(Parser.hostip))
				total_send += packet.size() - COOKED_HEAD_LEN;
			else if(dst.equals(Parser.hostip))
				total_recv += packet.size() - COOKED_HEAD_LEN;
		}	
	}
	
	public int getTotalSend()
	{
		return total_send;
	}
	public int getTotalRecv()
	{
		return total_recv;
	}
	public int getTotalBytes()
	{
		total = total_send + total_recv;
		return total;
	}

}
